//引入api
import { getAdminCategory, getTopCategory, postAddcategory, postPutimg } from '@/request/api.js';

//引入element
import { Message } from 'element-ui';


//引入路由
import router from '@/router'
export default {
    namespaced: true,
    state: {
        cateList: [],
        topcategoryList: [],
        subsign: {
            parent_id: '',
            name: '',
            wap_banner_url: '',
            is_show: false,
            sort_order: '',
            front_name: ''
        }
    },
    mutations: {
        categoryLists(state, payload) {
            state.cateList = payload
            console.log("categor里的", state.cateList)
        },
        goodsGetvalue(state, payload) {
            state.subsign = payload

        },
        goodseditsubsign(state, payload) {
            console.log('编辑前的参数', state.subsign)

            state.subsign = { ...payload }
            //对值做相应调整
            if (payload.is_show) {
                state.subsign.is_show = true
            } else {
                state.subsign.is_show = false
            }
        },
        //初始化参数
        goodscreatedsubsign(state, payload) {
            state.subsign = { ...payload }
        },
    },
    actions: {
        //发起请求
        goodsAddgoods({ commit, state }) {
            let is_show = 0;
            if (state.subsign.is_show) {
                is_show = 1
            } else {
                is_show = 0
            }
            postAddcategory({
                ...state.subsign,
                is_show
            }).then(res => {

                if (res) {
                    if(state.subsign.id){
                        Message({
                            message:'修改成功',
                            type:'success'
                        })
                    }else{
                        Message({
                            message:'添加成功',
                            type:'success'
                        })
                    }
                    router.push('/category')
                } else {
                    console.log('请求失败')
                }
            })
        },
        //获取分类数据
        categoryget({ commit, state }) {

            getAdminCategory({}).then(res => {
                console.log(res.data);
                commit('categoryLists', res.data)
            })
        },
        //获取顶部分类
        categorygetTop({ commit, state }) {
            getTopCategory().then((res) => {
                state.topcategoryList = res.data
                state.topcategoryList.unshift({ 'name': '顶级分类', 'id': 0 })
            })
        },
        //上传图片
        categoryPutimg({ commit, state }, payload) {
            console.log("查看传值", payload)
            postPutimg(
                payload
            ).then(res => {
                console.log(res.data)
                state.subsign.wap_banner_url = res.data.fileUrl

            })
        },
    }
}